package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.ReplyMapper;
import com.example.demo.nuoqi.pojo.Reply;
import com.example.demo.nuoqi.service.ReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Reply业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ReplyServiceImpl implements ReplyService {

    @Autowired
    private ReplyMapper replyMapper;


    /**
     * Reply条件+分页查询
     * @param reply 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Reply> findPage(Reply reply, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(reply);
        //执行搜索
        return new PageInfo<Reply>(replyMapper.selectByExample(example));
    }

    /**
     * Reply分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Reply> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Reply>(replyMapper.selectAll());
    }

    /**
     * Reply条件查询
     * @param reply
     * @return
     */
    @Override
    public List<Reply> findList(Reply reply){
        //构建查询条件
        Example example = createExample(reply);
        //根据构建的条件查询数据
        return replyMapper.selectByExample(example);
    }


    /**
     * Reply构建查询对象
     * @param reply
     * @return
     */
    public Example createExample(Reply reply){
        Example example=new Example(Reply.class);
        Example.Criteria criteria = example.createCriteria();
        if(reply!=null){
            // 
            if(!StringUtils.isEmpty(reply.getId())){
                    criteria.andEqualTo("id",reply.getId());
            }
            // 
            if(!StringUtils.isEmpty(reply.getProbleId())){
                    criteria.andEqualTo("probleId",reply.getProbleId());
            }
            // 
            if(!StringUtils.isEmpty(reply.getReplyContent())){
                    criteria.andEqualTo("replyContent",reply.getReplyContent());
            }
            // 
            if(!StringUtils.isEmpty(reply.getReplyName())){
                    criteria.andEqualTo("replyName",reply.getReplyName());
            }
            // 
            if(!StringUtils.isEmpty(reply.getReplyId())){
                    criteria.andEqualTo("replyId",reply.getReplyId());
            }
            // 
            if(!StringUtils.isEmpty(reply.getReplyCreteTime())){
                    criteria.andEqualTo("replyCreteTime",reply.getReplyCreteTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        replyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Reply
     * @param reply
     */
    @Override
    public void update(Reply reply){
        replyMapper.updateByPrimaryKey(reply);
    }

    /**
     * 增加Reply
     * @param reply
     */
    @Override
    public void add(Reply reply){
        replyMapper.insert(reply);
    }

    /**
     * 根据ID查询Reply
     * @param id
     * @return
     */
    @Override
    public Reply findById(Integer id){
        return  replyMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Reply全部数据
     * @return
     */
    @Override
    public List<Reply> findAll() {
        return replyMapper.selectAll();
    }
}
